package Clothing.serviceImpl.clothing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import Clothing.bean.request.clothing.AddClothingRequest;
import Clothing.bean.request.clothing.DeleteClothingRequest;
import Clothing.bean.request.clothing.GetClothingListRequest;
import Clothing.bean.respose.clothing.GetClothingListResponse;
import Clothing.bean.respose.clothing.GetClothingListResponse.Clothing;
import Clothing.dao.ClothingDao;
import Clothing.entity.ClothingEntity;
import Clothing.service.clothing.ClothingService;

@Service("clothingService")
public class ClothingServiceImpl implements ClothingService {

    @Autowired
    private ClothingDao clothingDao;
    
    public int getClothingList(GetClothingListRequest request, GetClothingListResponse response) {
        Map<String,Object> inputMap = new HashMap<String, Object>();
        inputMap.put("clothingName", request.getClothingName());
        inputMap.put("clothingType", request.getClothingType());
        List<ClothingEntity> clothingEntities = null;
        List<Clothing> clothingList = new ArrayList<Clothing>();
        
        clothingEntities = clothingDao.getClothingList(inputMap);
        if(clothingEntities == null){
            return 1;
        }
        for (ClothingEntity clothingEntity : clothingEntities) {
            Clothing clothing = new Clothing();
            BeanUtils.copyProperties(clothingEntity, clothing);
            clothingList.add(clothing);
        }
        response.setClothingInfo(clothingList);
        return 0;
    }

    public int addClothing(AddClothingRequest request) {
        ClothingEntity clothingEntity = new ClothingEntity();
        BeanUtils.copyProperties(request, clothingEntity);
        return clothingDao.insertSelective(clothingEntity);
    }

    public int deleteClothing(DeleteClothingRequest request) {
        if(StringUtils.isBlank(request.getClothingId())){
            return 1;
        }
        return clothingDao.deleteByPrimaryKey(Integer.parseInt(request.getClothingId()));
    }

    public int editClothing(AddClothingRequest request) {
        ClothingEntity clothingEntity = new ClothingEntity();
        BeanUtils.copyProperties(request, clothingEntity);
        clothingEntity.setClothingId(Integer.parseInt(request.getClothingId()));
        return clothingDao.updateByPrimaryKeySelective(clothingEntity);
    }

}
